<?php

class Account_logApp extends MemberbaseApp {

    var $_member_mod;
    var $_account_log_mod;
    var $_member_bank_mod;
    var $_order_mod;
    var $_user_id;

    function __construct() {
        $this->Account_logApp();
    }

    function Account_logApp() {
        parent::__construct();
        $this->_member_mod = & m('member');
        $this->_account_log_mod = & m('account_log');
        $this->_member_bank_mod = & m('member_bank');
        $this->_order_mod = & m('order');
        $this->_user_id = $this->visitor->get('user_id');
        $this->_user_name = $this->visitor->get('user_name');
    }

    function logall() {
        /* 当前用户中心菜单 */
        $this->_curitem('account_log');
        $this->_curmenu('account_logall');

        $conditions = $this->_get_query_conditions(array(
            array(
                'field' => 'add_time',
                'name' => 'add_time_from',
                'equal' => '>=',
                'handler' => 'gmstr2time',
            ), array(
                'field' => 'add_time',
                'name' => 'add_time_to',
                'equal' => '<=',
                'handler' => 'gmstr2time_end',
            ),
            array(//按订单号
                'field' => 'order_sn',
                'equal' => 'LIKE',
                'name' => 'order_sn',
            ),
            array(
                'field' => 'type',
                'equal' => '=',
                'name' => 'type',
                'type' => 'numeric',
            ),
            array(
                'field' => 'complete',
                'equal' => '=',
                'name' => 'complete',
                'type' => 'numeric',
            ),
        ));

        $account_type_list = array(
            ACCOUNT_TYPE_ADMIN => Lang::get('account_type_admin'), //手工操作
            ACCOUNT_TYPE_BUY => Lang::get('account_type_buy'), //购买商品
            ACCOUNT_TYPE_SELLER => Lang::get('account_type_seller'), //出售商品
            ACCOUNT_TYPE_IN => Lang::get('account_type_in'), //账户转入
            ACCOUNT_TYPE_OUT => Lang::get('account_type_out'), //账户转出
            ACCOUNT_TYPE_CZ => Lang::get('account_type_cz'), //账户充值
            ACCOUNT_TYPE_TX => Lang::get('account_type_tx'), //账户提现
            ACCOUNT_TYPE_REFUND_IN => Lang::get('account_type_refund_in'), //账户退款收入,通常为买家退款成功 得到退款
            ACCOUNT_TYPE_REFUND_OUT => Lang::get('account_type_refund_out'), //账户退款收入,通常为卖家退款成功 扣除退款
            ACCOUNT_TYPE_TUIJIAN_BUYER => Lang::get('account_type_tuijian_buyer'), // 用户推荐注册,注册者购买产品，推荐人会获得佣金，店铺会损失佣金。
            ACCOUNT_TYPE_TUIJIAN_SELLER => Lang::get('account_type_tuijian_seller'), // 用户推荐注册,注册者成为店主，卖出产品推荐人会获得佣金，店主会损失佣金。
            ACCOUNT_TYPE_TRADE_CHARGES => Lang::get('account_type_trade_charges'), // 扣除卖家交易佣金
        );
        $this->assign('account_type_list', $account_type_list);


        $page = $this->_get_page(10);
        $account_log_list = $this->_account_log_mod->find(array(
            'conditions' => 'user_id=' . $this->_user_id . $conditions,
            'limit' => $page['limit'],
            'order' => "id desc",
            'count' => true));
        $page['item_count'] = $this->_account_log_mod->getCount();
        $this->_format_page($page);
        $this->assign('filtered', $conditions ? 1 : 0); //是否有查询条件
        $this->assign('page_info', $page);

        foreach ($account_log_list as $key => $account_log) {
            $account_log_list[$key]['type_name'] = $account_type_list[$account_log['type']];
        }
        $this->assign('account_log_list', $account_log_list);

        $this->assign('complete_list', array(
            0 => Lang::get('uncomplete'),
            1 => Lang::get('oncomplete'),
        ));

        $this->import_resource(array(
            'script' => array(
                array(
                    'path' => 'jquery.ui/jquery.ui.js',
                    'attr' => '',
                ),
            ),
            'style' => 'jquery.ui/themes/ui-lightness/jquery.ui.css',
        ));
        /* 当前位置 */
        $this->_curlocal(LANG::get('member_center'), 'index.php?app=member', LANG::get('account_log'), 'index.php?app=account_log&act=logall', LANG::get('account_log'));
        $this->_config_seo('title', Lang::get('member_center') . ' - ' . Lang::get('account_logall'));
        $this->display('account_log.logall.html');
    }

    //在线充值
    function czlist() {
        /* 取得列表数据 */
        $model_payment = & m('payment');
        /* 获取白名单 */
        $white_list = $model_payment->get_white_list();
        /* 获取白名单过滤后的内置支付方式列表 */
        $payments = $model_payment->get_builtin($white_list);
        foreach ($payments as $payment_key => $payment) {
            $payments[$payment_key]['logo'] = SITE_URL . '/includes/payments/' . $payment_key . '/logo.gif';
            if (!$payment['is_online']) {
                unset($payments[$payment_key]);
            } else {
                if ($payment['is_mobile'] == 1) {
                    unset($payments[$payment_key]);
                }
            }
        }

        if (!IS_POST) {
            $this->_curitem('account_log');
            $this->_curmenu('account_log_czlist');
            $this->assign('payments', $payments);
            $member = $this->_member_mod->get("user_id=" . $this->_user_id);
            $this->assign('member', $member);
            /* 当前位置 */
            $this->_curlocal(LANG::get('member_center'), 'index.php?app=member', LANG::get('account_log'), 'index.php?app=account_log&act=logall', LANG::get('account_log_czlist'));
            $this->_config_seo('title', Lang::get('member_center') . ' - ' . Lang::get('account_log_czlist'));
            $this->display('account_log.czlist.html');
        } else {
            $order_info = array(
                'buyer_id' => $this->_user_id,
                'buyer_name' => $this->_user_name,
                'order_amount' => trim($_POST['cz_money']),
                'order_sn' => date('YmdHis', gmtime() + 8 * 3600) . rand(1000, 9999),
            );
            $czfs = trim($_POST['czfs']);
            $payment = $this->_get_payment($czfs, array());
            try {
                $payment_form = $payment->get_payform($order_info);
            } catch (WxPayException $e) {
                $query_string = explode('&', $_SERVER['QUERY_STRING']);
                foreach ($query_string as $key => $val) {
                    if ($val == 'code=' . $_GET['code'] || $val == 'state=' . $_GET['state']) {
                        unset($query_string[$key]);
                    }
                }

                $this->show_warning($e->getMessage(), 'go_back', "index.php?" . implode("&", $query_string));
                return;
            }
            $payment_info = $payments[$czfs];
            $payment_info['payment_code'] = $payment_info['code'];

            /* 跳转到真实收银台 */
            $this->_config_seo('title', Lang::get('member_center') . ' - ' . Lang::get('account_log'));
            $this->assign('payform', $payment_form);
            $this->assign('payment', $payment_info);
            $this->assign('order', $order_info);
            header('Content-Type:text/html;charset=' . CHARSET);
            /* 线下付款的 */
            if (!$payment_info['is_online']) {
                $this->_curlocal(Lang::get('post_pay_message'));
                $this->display('cashier.payform.html'); #线下付款调用的模板
            } else {
                $this->display('cashier.online.html'); #线上付款调用的模板
            }
        }
    }

    //提现申请
    function withdraw() {
        if (!IS_POST) {
            $this->_curitem('account_log');
            $this->_curmenu('account_log_withdraw');
            $member = $this->_member_mod->get($this->_user_id);
            if (empty($member['zf_pass'])) {
                $this->show_message('account_log_editpassword', 'account_log_editpassword', 'index.php?app=account_log&act=editpassword');
                return;
            }
            $this->assign('member', $member);
            //获取当前用户设置的银行卡信息
            $bank_list = $this->_member_bank_mod->find(array('conditions' => 'user_id=' . $this->_user_id));
            $this->assign('bank_list', $bank_list);
            /* 当前位置 */
            $this->_curlocal(LANG::get('member_center'), 'index.php?app=member', LANG::get('account_log'), 'index.php?app=account_log&act=logall', LANG::get('account_log_withdraw'));
            $this->_config_seo('title', Lang::get('member_center') . ' - ' . Lang::get('account_log_withdraw'));
            $this->display('account_log.withdraw.html');
        } else {
            $tx_money = trim($_POST['tx_money']);
            $this->_member_mod->db->query('start transaction');
            $money_row = $this->_member_mod->getAll('SELECT * FROM '.$this->_member_mod->table.' WHERE user_id='.$this->_user_id.' FOR UPDATE');
            if(!is_array($money_row) || empty($money_row)){
                $this->_member_mod->db->query('rollback');
                $this->show_warning('用户不存在');
                return;
            }
            $money_row= current($money_row);
            $post_zf_pass = trim($_POST['post_zf_pass']);
            if (empty($post_zf_pass)) {
                $this->_member_mod->db->query('rollback');
                $this->show_warning('cuowu_zhifumimabunengweikong');
                return;
            }
            $md5zf_pass = md5($post_zf_pass);
            if ($money_row['zf_pass'] != $md5zf_pass) {
                $this->_member_mod->db->query('rollback');
                $this->show_warning('cuowu_zhifumimayanzhengshibai');
                return;
            }
            //检测用户的银行信息
            if (empty($tx_money)) {
                $this->_member_mod->db->query('rollback');
                $this->show_warning('cuowu_tixianjinebunengweikong');
                return;
            }
            if (preg_match("/[^0.-9]/", $tx_money)) {
                $this->_member_mod->db->query('rollback');
                $this->show_warning('cuowu_nishurudebushishuzilei');
                return;
            }
            if ($money_row['money'] < $tx_money) {
                $this->_member_mod->db->query('rollback');
                $this->show_warning('duibuqi_zhanghuyuebuzu');
                return;
            }
            //通过验证 开始操作数据
            $newmoney = $money_row['money'] - $tx_money;
            $newmoney_dj = $money_row['money_dj'] + $tx_money;

            //获取提交的bank_id
            $bank_id = $_POST['bank_id'];
            if(empty($bank_id)){
                $this->_member_mod->db->query('rollback');
                $this->show_warning('bank_empty');
                return;
            }
            $bank = $this->_member_bank_mod->get('bank_id=' . $bank_id . ' AND user_id=' . $this->_user_id);
            if(empty($bank)){
                $this->_member_mod->db->query('rollback');
                $this->show_warning('bank_empty');
                return;
            }
            $bank_str = ' 提现:' . $bank['bank_name'] .'-'. $bank['bank_type'] . ',户名:' . $bank['account_name'] . ',账号:' . $bank['bank_num'];

            //添加日志
            $order_sn = date('YmdHis', gmtime() + 8 * 3600) . rand(1000, 9999);
            $log_text = $this->_user_name . Lang::get('tixianshenqingjine') . $tx_money . Lang::get('yuan') . $bank_str;

            $add_account_log = array(
                'user_id' => $this->_user_id,
                'user_name' => $this->_user_name,
                'order_sn' => $order_sn,
                'add_time' => gmtime(),
                'type' => ACCOUNT_TYPE_TX, //提现
                'money_flow' => 'outlay',
                'money' => $tx_money,
                'log_text' => $log_text,
                'states' => 70, //待审核																			
            );
            
            $mysql_flag=$this->_account_log_mod->add($add_account_log);
            if(!$mysql_flag){
                $this->_member_mod->db->query('rollback');
                $this->show_warning('系统繁忙，请重试');return;
            }
            $edit_mymoney = array(
                'money_dj' => $newmoney_dj,
                'money' => $newmoney,
            );
            $mysql_flag=$this->_member_mod->edit('user_id=' . $this->_user_id, $edit_mymoney);
            if(!$mysql_flag){
                $this->_member_mod->db->query('rollback');
                $this->show_warning('系统繁忙，请重试');return;
            }else{
                $this->_member_mod->db->query('commit');
            }
            $this->show_message('tixian_chenggong');
            return;
        }
    }

//余额转帐
    function out() {
        $to_user = trim($_POST['to_user']);
        $to_money = trim($_POST['to_money']);
        if (!IS_POST) {
            /* 当前用户中心菜单 */
            $this->_curitem('account_log');
            $this->_curmenu('account_log_out');
            $member = $this->_member_mod->get("user_id=" . $this->_user_id);
            $this->assign('member', $member);
            /* 当前位置 */
            $this->_curlocal(LANG::get('member_center'), 'index.php?app=member', LANG::get('account_log'), 'index.php?app=account_log&act=logall', LANG::get('account_log_out'));
            $this->_config_seo('title', Lang::get('member_center') . ' - ' . Lang::get('account_log_out'));
            $this->display('account_log.out.html');
        } else {//检测有提交
            if (preg_match("/[^0.-9]/", $to_money)) {
                $this->show_warning('cuowu_nishurudebushishuzilei');
                return;
            }
            $this->_member_mod->db->query('start transaction');
            $to_row = $this->_member_mod->getAll('SELECT * FROM '.$this->_member_mod->table.' WHERE user_name="'.$to_user.'" FOR UPDATE');
            if(!is_array($to_row) || empty($to_row)){
                $this->_member_mod->db->query('rollback');
                $this->show_warning('用户不存在');
                return;
            }
            $to_row= current($to_row);
            $to_user_id = $to_row['user_id'];
            $to_user_name = $to_row['user_name'];
            $to_user_money = $to_row['money'];

            if ($to_user_id == $this->_user_id) {
                $this->_member_mod->db->query('rollback');
                $this->show_warning('cuowu_bunenggeizijizhuanzhang');
                return;
            }

            if (empty($to_user_id)) {
                $this->_member_mod->db->query('rollback');
                $this->show_warning('cuowu_mubiaoyonghubucunzai');
                return;
            }
            
            $member = $this->_member_mod->getAll('SELECT * FROM '.$this->_member_mod->table.' WHERE user_id='.$this->_user_id.' FOR UPDATE');
            if(!is_array($member) || empty($member)){
                $this->_member_mod->db->query('rollback');
                $this->show_warning('用户不存在');
                return;
            }
            $member= current($member);
            $user_money = $member['money'];
            $user_zf_pass = $member['zf_pass'];
            $zf_pass = md5(trim($_POST['zf_pass']));
            if ($user_zf_pass != $zf_pass) {
                $this->_member_mod->db->query('rollback');
                $this->show_warning('cuowu_zhifumimayanzhengshibai');
                return;
            }
            $time = gmtime();
            $order_sn = date('YmdHis', $time + 8 * 3600) . rand(1000, 9999);
            if ($user_money < $to_money) {
                $this->_member_mod->db->query('rollback');
                $this->show_warning('cuowu_zhanghuyuebuzu');
                return;
            } else {
                
                //添加日志
                $log_text = $this->_user_name . Lang::get('gei') . $to_user . Lang::get('zhuanchujine') . $to_money . Lang::get('yuan');
                $add_account_log = array(
                    'user_id' => $this->_user_id,
                    'user_name' => $this->_user_name,
                    'order_sn' => $order_sn,
                    'add_time' => $time,
                    'type' => ACCOUNT_TYPE_OUT, //转出	
                    'money_flow' => 'outlay',
                    'money' => $to_money,
                    'complete' => 1,
                    'log_text' => $log_text,
                    'states' => 40,
                );
                $mysql_flag=$this->_account_log_mod->add($add_account_log);
                if(!$mysql_flag){
                    $this->_member_mod->db->query('rollback');
                    $this->show_warning('系统繁忙，请重试');return;
                }
                $log_text_to = $this->_user_name . Lang::get('gei') . $to_user_name . Lang::get('zhuanrujine') . $to_money . Lang::get('yuan');
                $add_account_log_to = array(
                    'user_id' => $to_user_id,
                    'user_name' => $to_user_name,
                    'order_sn' => $order_sn,
                    'add_time' => $time,
                    'type' => ACCOUNT_TYPE_IN, //转入	
                    'money_flow' => 'income',
                    'money' => $to_money,
                    'complete' => 1,
                    'log_text' => $log_text_to,
                    'states' => 40,
                );
                $mysql_flag=$this->_account_log_mod->add($add_account_log_to);
                if(!$mysql_flag){
                    $this->_member_mod->db->query('rollback');
                    $this->show_warning('系统繁忙，请重试');return;
                }
                $new_user_money = $user_money - $to_money;
                $new_to_user_money = $to_user_money + $to_money;

                $add_jia = array(
                    'money' => $new_to_user_money,
                );
                $mysql_flag=$this->_member_mod->edit('user_id=' . $to_user_id, $add_jia);
                if(!$mysql_flag){
                    $this->_member_mod->db->query('rollback');
                    $this->show_warning('系统繁忙，请重试');return;
                }
                $add_jian = array(
                    'money' => $new_user_money,
                );
                $mysql_flag=$this->_member_mod->edit('user_id=' . $this->_user_id, $add_jian);
            if(!$mysql_flag){
                $this->_member_mod->db->query('rollback');
                $this->show_warning('系统繁忙，请重试');return;
            }else{
                $this->_member_mod->db->query('commit');
            }
                $this->show_message('zhuanzhangchenggong');
                return;
            }
        }
    }

    //修改支付密码
    function editpassword() {
        $member = $this->_member_mod->get("user_id=" . $this->_user_id);
        if (!$_POST) {//检测是否提交
            $this->assign('member', $member);
            /* 当前用户中心菜单 */
            $this->_curitem('account_log');
            $this->_curmenu('account_log_editpassword');
            /* 当前位置 */
            $this->_curlocal(LANG::get('member_center'), 'index.php?app=member', LANG::get('account_log'), 'index.php?app=account_log&act=logall', LANG::get('account_log_editpassword'));
            $this->_config_seo('title', Lang::get('member_center') . ' - ' . Lang::get('account_log_editpassword'));
            $this->display('account_log.editpassword.html');
            return;
        } else {
            $y_pass = trim($_POST['y_pass']);
            $zf_pass = trim($_POST['zf_pass']);
            $zf_pass2 = trim($_POST['zf_pass2']);
            if (empty($zf_pass)) {
                $this->show_warning('cuowu_zhifumimabunengweikong');
                return;
            }
            if ($zf_pass != $zf_pass2) {
                $this->show_warning('cuowu_liangcishurumimabuyizhi');
                return;
            }
            //如果未设置支付密码，则直接设置,已设置支付密码 需要验证原支付密码
            $md5zf_pass = md5($zf_pass);
            if ($member['zf_pass'] != "") {
                //转换32位 MD5
                $md5y_pass = md5($y_pass);

                if ($member['zf_pass'] != $md5y_pass) {
                    $this->show_warning('cuowu_yuanzhifumimayanzhengshibai');
                    return;
                }
            }
            $newpass_array = array(
                'zf_pass' => $md5zf_pass,
            );
            $this->_member_mod->edit('user_id=' . $this->_user_id, $newpass_array);
            $this->show_message('zhifumimaxiugaichenggong');
            return;
        }
    }

    function bank_add() {
        if (!IS_POST) {
            $this->_curitem('account_log');
            $this->_curmenu('account_log_add_bank');
            $this->assign('bank', array('card_type' => 'bank'));
            /* 当前位置 */
            $this->_curlocal(LANG::get('member_center'), 'index.php?app=member', LANG::get('account_log'), 'index.php?app=account_log&act=logall', LANG::get('account_log_add_bank'));
            $this->_config_seo('title', Lang::get('member_center') . ' - ' . Lang::get('account_log_add_bank'));
            $this->display('account_log.bank_form.html');
        } else {
            $account_name = trim($_POST['account_name']);
            $card_type = trim($_POST['card_type']);
            $bank_num = trim($_POST['bank_num']);

            if (empty($bank_num)) {
                $this->show_warning('num_empty');
                return;
            }
            if (empty($account_name) || strlen($account_name) < 6 || strlen($account_name) > 30) {
                $this->show_warning('account_name_error');
                return;
            }
            if($card_type == 'alipay'){
                $bank_info['bank_name'] = Lang::get('alipay');
                $bank_info['bank_type'] =Lang::get('Alibaba');
            }else if($card_type == 'bank'){
                $bank_info = $this->_get_bank_info($bank_num);
            }else{
                $this->show_warning('type_error');
                return;
            }
            $data = array(
                'user_id' => $this->_user_id,
                'bank_name' => $bank_info['bank_name'],
                'account_name' => $account_name,
                'bank_type'=>$bank_info['bank_type'],
                'bank_num' => $bank_num,
                'card_type'=>$card_type,
            );

            if (!$this->_member_bank_mod->add($data)) {
                $this->show_warning('add_error');
                return;
            }
            $this->show_message('add_ok', 'back_list', 'index.php?app=account_log&act=withdraw');
        }
    }
    
    

    function bank_edit() {
        $bank_id = empty($_GET['bank_id']) ? 0 : intval($_GET['bank_id']);
        $bank = $this->_member_bank_mod->get('bank_id=' . $bank_id . ' AND user_id=' . $this->_user_id);
        if (empty($bank)) {
            $this->show_warning('error');
            return;
        }
        if (!IS_POST) {
            $this->_curitem('account_log');
            $this->_curmenu('bank_edit');
            $this->assign('bank', $bank);
            /* 当前位置 */
            $this->_curlocal(LANG::get('member_center'), 'index.php?app=member', LANG::get('account_log'), 'index.php?app=account_log&act=logall', LANG::get('bank_edit'));
            $this->_config_seo('title', Lang::get('member_center') . ' - ' . Lang::get('bank_edit'));
            $this->display('account_log.bank_form.html');
        } else {
            $account_name = trim($_POST['account_name']);
            $card_type = trim($_POST['card_type']);
            $bank_num = trim($_POST['bank_num']);

            if (empty($bank_num)) {
                $this->show_warning('num_empty');
                return;
            }
            if (empty($account_name) || strlen($account_name) < 6 || strlen($account_name) > 30) {
                $this->show_warning('account_name_error');
                return;
            }
            if($card_type == 'alipay'){
                $bank_info['bank_name'] = Lang::get('alipay');
                $bank_info['bank_type'] =Lang::get('Alibaba');
            }else if($card_type == 'bank'){
                $bank_info = $this->_get_bank_info($bank_num);
            }else{
                $this->show_warning('type_error');
                return;
            }
            $data = array(
                'user_id' => $this->_user_id,
                'bank_name' => $bank_info['bank_name'],
                'account_name' => $account_name,
                'bank_type'=>$bank_info['bank_type'],
                'bank_num' => $bank_num,
                'card_type'=>$card_type,
            );

            if (!$this->_member_bank_mod->edit($bank_id, $data)) {
                $this->show_warning('edit_error');
                return;
            }
            $this->show_message('edit_ok', 'back_list', 'index.php?app=account_log&act=withdraw');
        }
    }
    
    function _get_bank_info($bank_num) {
        $bank_list = include ROOT_PATH . '/data/bank.inc.php';
        if (!is_array($bank_list) || count($bank_list) < 1) {
            $this->show_warning('bank_inc_error');
            exit;
        }
        $bank_num_8 = substr($bank_num, 0, 8);
        if (isset($bank_list[$bank_num_8])) {
            $value = $bank_list[$bank_num_8];
        }
        $bank_num_6 = substr($bank_num, 0, 6);
        if (isset($bank_list[$bank_num_6])) {
            $value = $bank_list[$bank_num_6];
        }
        $bank_num_5 = substr($bank_num, 0, 5);
        if (isset($bank_list[$bank_num_5])) {
            $value = $bank_list[$bank_num_5];
        }
        $bank_num_4 = substr($bank_num, 0, 4);
        if (isset($bank_list[$bank_num_4])) {
            $value = $bank_list[$bank_num_4];
        }
        if(empty($value)){
            $this->show_warning('bank_num_error');
            exit;
        }
        $result = explode('-', $value,2);
        if(empty($result[0])||empty($result[1])){
            $this->show_warning('bank_num_error');
            exit;
        }
        $bank_info['bank_name'] = $result[0];
        $bank_info['bank_type'] = $result[1];
        return $bank_info;
    }

    function _get_member_submenu() {
        $menus = array(
            array(
                'name' => 'account_logall',
                'url' => 'index.php?app=account_log&act=logall',
            ),
            array(
                'name' => 'account_log_czlist',
                'url' => 'index.php?app=account_log&act=czlist',
            ),
            array(
                'name' => 'account_log_out',
                'url' => 'index.php?app=account_log&act=out',
            ),
            array(
                'name' => 'account_log_editpassword',
                'url' => 'index.php?app=account_log&act=editpassword',
            ),
            array(
                'name' => 'account_log_withdraw',
                'url' => 'index.php?app=account_log&act=withdraw',
            ),
        );
        return $menus;
    }

    //支付定单
    function payment() {
        /* 外部提供订单号 */
        $order_id_array=  explode(',', isset($_GET['order_id']) ? trim($_GET['order_id']) : '');
        $order_model = & m('order');
        $all_order_amount=0;
        $all_order_sn='';
        foreach($order_id_array as $fe_order_id){
            
            $order_id=intval($fe_order_id);
        if (!$order_id) {
            $this->show_warning('no_such_order');
            return;
        }
        /* 内部根据订单号收银,获取收多少钱，使用哪个支付接口 */
        $order_info = $order_model->get("order_id={$order_id} AND buyer_id=" . $this->_user_id);
        if (empty($order_info)) {
            $this->show_warning('no_such_order');
            return;
        }
        /* 订单有效性判断 */
        if ($order_info['payment_code'] != 'cod' && $order_info['status'] != ORDER_PENDING) {
            $this->show_warning('no_such_order');
            return;
        }
            $all_order_amount= bcadd($all_order_amount, floatval($order_info['order_amount']),2);
            $all_order_sn.=($all_order_sn?'、':'').$order_info['order_sn'];
        }
        $member = $this->_member_mod->get("user_id=" . $this->_user_id);
        if (empty($member['zf_pass'])) {
            $this->_member_mod->edit("user_id=" . $this->_user_id, array('zf_pass' => md5('123456')));
        }
        if ($_POST) {
            $zf_pass = trim($_POST['zf_pass']);
            $post_money = trim($_POST['post_money']); #提交过来的订单金额
            //检测支付密码
            if (empty($zf_pass)) {
                $this->show_warning('cuowu_zhifumimabunengweikong');
                return;
            }
            $md5zf_pass = md5($zf_pass);
            if ($member['zf_pass'] != $md5zf_pass) {
                $this->show_warning('cuowu_zhifumimayanzhengshibai');
                return;
            }
            //检测余额是否足够
            if ($member['money'] < $all_order_amount) {
                $this->show_warning('cuowu_zhanghuyuebuzu', 'lijichongzhi', 'index.php?app=account_log&act=czlist');
                return;
            }
            //金额是否相同
            if ($post_money != $all_order_amount) {
                $this->show_warning('fashengcuowu_jineshujukeyi');
                return;
            }
        foreach($order_id_array as $fe_order_id){
            $this->_member_mod->db->query('start transaction');
            $member = $this->_member_mod->getAll('SELECT * FROM '.$this->_member_mod->table.' WHERE user_id='.$this->_user_id.' FOR UPDATE');
            if(!is_array($member) || empty($member)){
                $this->_member_mod->db->query('rollback');
                $this->show_warning('用户不存在');
                return;
            }
            $member= current($member);
            $order_id=intval($fe_order_id);
            $order_info = $order_model->get("order_id={$order_id} AND buyer_id=" . $this->_user_id);
            //读取卖家SQL
            $seller_row = $this->_member_mod->getAll('SELECT * FROM '.$this->_member_mod->table.' WHERE user_id='.$order_info['seller_id'].' FOR UPDATE');
            if(!is_array($seller_row) || empty($seller_row)){
                $this->_member_mod->db->query('rollback');
                $this->show_warning('seller_not_open_payment');
                return;
            }
            $seller_row= current($seller_row);
            $seller_id = $seller_row['user_id']; #卖家ID 
            $seller_name = $seller_row['user_name']; #卖家用户名
            $seller_money_dj = $seller_row['money_dj']; #卖家的原始冻结金钱
            //扣除买家的金钱
            $buyer_array = array(
                'money' => $member['money'] - $order_info['order_amount'],
            );
            
            $mysql_flag=$this->_member_mod->edit('user_id=' . $this->_user_id, $buyer_array);
            if(!$mysql_flag){
                $this->_member_mod->db->query('rollback');
                continue;
            }
            //更新卖家的冻结金钱	
            $seller_array = array(
                'money_dj' => $seller_money_dj + $order_info['order_amount'],
            );
            
            $mysql_flag=$this->_member_mod->edit($seller_id, $seller_array);
            if(!$mysql_flag){
                $this->_member_mod->db->query('rollback');
                continue;
            }
            //买家添加日志
            $time = gmtime();
            $buyer_log_text = Lang::get('goumaishangpin_dianzhu') . $seller_name;
            $buyer_add_array = array(
                'user_id' => $this->_user_id,
                'user_name' => $member['user_name'],
                'order_id' => $order_id,
                'order_sn' => $order_info['order_sn'],
                'add_time' => $time,
                'type' => ACCOUNT_TYPE_BUY,
                'money_flow' => 'outlay',
                'money' => $order_info['order_amount'],
                'log_text' => $buyer_log_text,
                'states' => 20,
            );
            $mysql_flag=$this->_account_log_mod->add($buyer_add_array);
            if(!$mysql_flag){
                $this->_member_mod->db->query('rollback');
                continue;
            }
            //卖家添加日志
            $seller_log_text = Lang::get('chushoushangpin_maijia') . $member['user_name'];
            $seller_add_array = array(
                'user_id' => $seller_id,
                'user_name' => $seller_name,
                'order_id' => $order_id,
                'order_sn' => $order_info['order_sn'],
                'add_time' => $time,
                'type' => ACCOUNT_TYPE_SELLER,
                'money_flow' => 'income',
                'money' => $order_info['order_amount'],
                'log_text' => $seller_log_text,
                'states' => 20,
            );
            $mysql_flag=$this->_account_log_mod->add($seller_add_array);
            if(!$mysql_flag){
                $this->_member_mod->db->query('rollback');
                continue;
            }
            //改变定单为 已支付等待卖家确认  status10改为20
            $payment_code = "zjgl";
            //更新定单状态
            $order_edit_array = array(
                'payment_name' => Lang::get('zjgl'),
                'payment_code' => $payment_code,
                'pay_time' => $time,
                'status' => ORDER_ACCEPTED, //20就是 待发货了
                'pd_amount'=>0
            );
            $mysql_flag=$this->_order_mod->edit($order_id, $order_edit_array);
            if(!$mysql_flag){
                $this->_member_mod->db->query('rollback');
                continue;
            }else{
                $this->_member_mod->db->query('commit');
            }
            /* 发送邮件给卖家，提醒付款成功 */
            $mail = get_mail('toseller_online_pay_success_notify', array('order' => $order_info));
            $this->_mailto($seller_row['email'], addslashes($mail['subject']), addslashes($mail['message']));
            /* 同步发送 */
            $this->_sendmail(true);
            }
            
            //支付成功
            $this->show_message('zhifu_chenggong', 'sanmiaohouzidongtiaozhuandaodingdanliebiao', 'index.php?app=buyer_order', 'chankandingdan', 'index.php?app=buyer_order', 'guanbiyemian', 'index.php?app=account_log&act=exits');
        } else {
            //余额支付 读取资金，读取店铺是否安装支付方式
            
            $this->assign('money', $member);
            $this->assign('order', array('order_id'=>$_GET['order_id'],'order_amount'=>$all_order_amount,'order_sn'=>$all_order_sn));
            $this->display('cashier.payment1.html');
        }
    }

    function findpassword() {
        if (!IS_POST) {
            $this->_curitem('account_log');
            $this->_curmenu('account_log_findpassword');
            $this->_config_seo('title', Lang::get('member_center') . ' - ' . Lang::get('account_log_findpassword'));


            if (Conf::get('msg_enabled')) {
                $this->assign('by_mobile', true);
            }

            $this->display('account_log.findpassword.html');
        } else {
            $find_way = intval($_POST['find_way']);
            $member_mod = & m('member');
            $member = $member_mod->get($this->visitor->get('user_id'));
            $new_password = $this->createNonceStr(6);
            if ($find_way == 1) {

                if ($member['email']) {
                    import('mailer.lib');
                    $model_setting = &af('settings');
                    $setting = $model_setting->getAll();
                    if ($setting['email_type'] && $setting['email_addr'] && $setting['email_host'] && $setting['email_port'] && $setting['email_id'] && $setting['email_pass']) {
                        $mailer = new Mailer(Lang::get('from2'), $setting['email_addr'], $setting['email_type'], $setting['email_host'], $setting['email_port'], $setting['email_id'], $setting['email_pass'], $setting['email_secure']);
                        $email_content =  Lang::get('you_new_password') . $new_password .Lang::get('please_modify_intime');
                        $mail_result = $mailer->send($member['email'], Lang::get('account_log_findpassword'), $email_content, CHARSET, 1);
                        if ($mail_result) {
                            $member_mod->edit($this->visitor->get('user_id'), array(
                                'zf_pass' => md5($new_password)
                            ));
                            $this->show_message(Lang::get('has_send_msg_from_you') . $member['email'] . Lang::get('please_note_check')
                            );
                            return;
                        } else {
                            $this->show_warning(Lang::get('mail_send_fail') . implode("\n", $mailer->errors));
                            return;
                        }
                    } else {
                        $this->show_warning('notify_admin_configure');
                        return;
                    }
                } else {
                    $this->show_warning('no_binding_mail');
                    return;
                }
            }
            if ($find_way == 2) {
                if ($member['phone_mob']) {
                    import('mobile_msg.lib');
                    $mobile_msg = new Mobile_msg();

                    $mobile_result = $mobile_msg->send_msg($this->visitor->get('user_id'), $this->visitor->get('user_name'), $member['phone_mob'], Lang::get("you_new_password") . $new_password . Lang::get("please_modify_intime"));
                    if ($mobile_result['done']) {
                        $member_mod->edit($this->visitor->get('user_id'), array(
                            'zf_pass' => md5($new_password)
                        ));
                        $this->show_message(Lang::get('has_send_msg_from_you2') . $member['phone_mob'] . Lang::get('please_note_check'));
                        return;
                    } else {
                        $this->show_warning('msg_send_fail');
                        return;
                    }
                } else {
                    $this->show_warning('no_binding_phone');
                    return;
                }
            }
        }
    }

    private function createNonceStr($length = 16) {
        $chars = "abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ0123456789";
        $str = "";
        for ($i = 0; $i < $length; $i++) {
            $str .= substr($chars, mt_rand(0, strlen($chars) - 1), 1);
        }
        return $str;
    }
}

?>
